package com.example.filter;

import java.io.IOException;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;

@WebFilter("/*")
public class FilterDemo implements Filter {

    // init
    // doFilter
    // destory
    // 过滤器配置完成后 在web.xml 或 @WebFilter注解 中配置拦截规则 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器: 初始化");
    }

    // 过滤
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        System.out.println(request.getLocalAddr());
        response.setContentType("text/html;charset=UTF-8");
        //过滤出口 加上过滤代码 交给下一级调用
        chain.doFilter(request, response);

    }

    // 销毁
    @Override
    public void destroy() {
        System.out.println("过滤器: 销毁");
    }

}
